﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using MODEL;
namespace winUi
{
    public partial class FormClass : Form
    {
        private int editId = 0; 
        public FormClass()
        {
            InitializeComponent();
        }

        private void FormClass_Load(object sender, EventArgs e)
        {

            LoadData();
        }

        void LoadData()
        {
            tvClass.Nodes.Clear();
            TreeNode fnode = new TreeNode("分类");
            fnode.Tag = 0;
            tvClass.Nodes.Add(fnode);
            List<Class> classs = new BllClass().GetAll();
            foreach (Class c in classs)
            {
                
                TreeNode node = new TreeNode(c.ClassName);
                node.Tag = c.Id;
                tvClass.Nodes.Add(node);
            }
        }
        void clear()
        {
            txtName.Text = "";
        }
        private void button1_Click(object sender, EventArgs e)
        {
            
            if (txtName.Text == "")
            {
                MessageBox.Show("名称不能为空");
                return;
            }
            if (new BllClass().IsExistByName(txtName.Text) && editId == 0)
            {
                MessageBox.Show("已经存在了！");
                return;
            }
            
            Class c = new Class();
            c.Pid = 0;
            c.ClassName = txtName.Text;
            c.Type = "分类";
            try
            {
                int result = 0;
                if (editId == 0)
                {
                    result = new BllClass().Insert(c);
                }
                else
                {
                    c.Id = editId;
                    result = new BllClass().Update(c);
                }
              
              if (result>0)
                {
                    MessageBox.Show("保存成功");
                    clear();
                    LoadData();
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }

        }

        private void tvClass_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            tvClass.SelectedNode = e.Node;
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (MessageBox.Show("你要删除分类吗", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                new BllClass().Delete(tvClass.SelectedNode.ImageIndex);
                LoadData();
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            clear();
        }

        private void 编辑ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            txtName.Text = tvClass.SelectedNode.Text;
            button1.Text = "保存";
            //flagNew = false;
            editId = (int)tvClass.SelectedNode.Tag;
        }
    }
}
